#define  _NAVI_BOARD_GLOBAL_
#include "config.h"

#include "MCUUpdate.c"

void conNaviBoardReqMCUFileStatus(void)
{
	BYTE buff[1];
	buff[0] = 0;

	printf("bootloader conNaviBoardReqMCUFileStatus\r\n");
	antProcNaviBoardWithAPPIDSend(0x07,0x10,buff,sizeof(buff));
}

void conNaviBoardReqMCUFileIndex(U32 index)
{
	BYTE buff[4];
	forU32TopU8LSB(index,buff);

	printf("bootloader conNaviBoardReqMCUFileIndex %d\r\n",index);
	antProcNaviBoardWithAPPIDSend(0x07,0x11,buff,sizeof(buff));
}

void conNaviBoardReportMCUStatus(U8 status)
{
	BYTE buff[1];
	buff[0] = status;

	printf("bootloader conNaviBoardReportMCUStatus %d\r\n",status);
	antProcNaviBoardWithAPPIDSend(0x07,0x12,buff,sizeof(buff));
}

void MCUUpdateMsgReqProc(BOOL bNow)
{
	if (bNow || GetTickCount() - naviBoardInfo.MCUUpdateRecTime >= T_1S)
	{
		naviBoardInfo.MCUUpdateRecTime = GetTickCount();

		if (!naviBoardInfo.MCUUpdateIng)
		{
			conNaviBoardReqMCUFileStatus();
		}
		else
		{
			conNaviBoardReqMCUFileIndex(naviBoardInfo.MCUUpdateIndex);
		}
	}
}

void naviBoardFreeIdleProc(void)
{
	U8 iRxData;
	if (Uart2_RxEn)
	{
		while (FIFO_GET_ONE(Uart2RxFifo,&iRxData))
		{
			AntProcProc(iRxData);
		}
	}

	MCUUpdateMsgReqProc(FALSE);
}

void dealNaviBoardData(U8 *pData,U8 len)
{
    uint8_t appID    = pData[0];
    uint8_t appCmd   = pData[1];
    uint8_t *param   = &pData[2];
    uint8_t paramCnt = len-2;

	//printf("dealNaviBoardData: %x %x %d\r\n",appID,appCmd,param[0]);

	if (0x07 == appID)
	{
		testLEDBlink();
		if (0x10 == appCmd)
		{
			U8 magicString[9];
			memcpy(magicString,&param[8],8);
			magicString[8] = 0;
			printf("maticString:%s \r\n",pMagicString);
			if (0 == memcmp(pMagicString,magicString,8))
			{
				printf("dealNaviBoardData 10 : size:%d crc:%x framecount:%d \r\n",naviBoardInfo.MCUHexSize,naviBoardInfo.MCUHexCRC,naviBoardInfo.MCUHexFrameCount);
			naviBoardInfo.MCUHexSize = forU8ToU32LSB(&param[0]);
			naviBoardInfo.MCUHexCRC = forU8ToU32LSB(&param[4]);
			naviBoardInfo.MCUHexFrameCount = (naviBoardInfo.MCUHexSize+127)/128;

			if (!naviBoardInfo.MCUUpdateIng)
			{
				if (naviBoardInfo.MCUHexSize != *(U32 *)SYSTEM_CFG_FILE_SIZE || naviBoardInfo.MCUHexCRC != *(U32 *)SYSTEM_CFG_FILE_CRC)
				{
					conNaviBoardReportMCUStatus(1);

					naviBoardInfo.MCUUpdateIng = TRUE;
					naviBoardInfo.MCUUpdateIndex = 0;
					
					MCUUpdateInit();
					mcuUpdateInfo.calcMCUHexCRC = naviBoardInfo.MCUHexSize;
					}
				}
			}
		}
		else if (0x11 == appCmd)
		{
			U32 frameIndex = forU8ToU32LSB(&param[0]);
			if (frameIndex == naviBoardInfo.MCUUpdateIndex)
			{
				U8 i;
				printf("bootloader update index %d %d\r\n",frameIndex,naviBoardInfo.MCUHexFrameCount);

				for (i = 4;i < paramCnt;i++)
				{
					mcuUpdateInfo.calcMCUHexCRC += param[i]<<(param[i]&0x0F);
					printf("%c",param[i]);
				}
				
				MCUUpdateProc(&param[4],paramCnt-4);
				naviBoardInfo.MCUUpdateIndex++;
				MCUUpdateMsgReqProc(TRUE);
				naviBoardInfo.MCUUpdateRecTime = GetTickCount();
			}
		}
	}
}

void naviBoardInit(void)
{
	Uart2Init(115200);
	memset(&naviBoardInfo,0,sizeof(NAVI_BOARD_INFO));
	AntProc_Init();

    printf(pMagicString);
	conNaviBoardReportMCUStatus(0);
}

